package com.cwvs.cat.bean;

import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import com.avos.avoscloud.AVFile;
import com.avos.avoscloud.AVGeoPoint;
import com.avos.avoscloud.AVUser;


public class User extends AVUser {
	public String token="";//2b533a714023951a287758f7fcfa5c06",
	public String id="";//5",
	public String username="";//15371862380",
	public String passwd="";//96e79218965eb72c92a549dd5a330112",
	public String nickname="";//null,
	public String avatar="";//null,
	public String gender="";// "0",
	public String sign="";//null,
	public String location_x="";// "120.7386490000",
	public String location_y="";// "31.2705770000",
	public String location_time="";// null,
	public String location_status="";// "0",
	public String modifed="";//15-03-19 10:15:05"

	public static final String USERNAME = "username";
	public static final String PASSWORD = "password";
	public static final String AVATAR = "avatar";
	public static final String FRIENDS = "friends";
	public static final String LOCATION = "location";
	public static final String GENDER = "gender";
	private static User curUser;
	private String sortLetters;
	public User(){

	}
	public User(List<String> list){
		this.id=  list.get(0);
		this.username=  list.get(1);
		this.passwd=  list.get(2);
		this.nickname=  list.get(3);
		this.avatar=  list.get(4);
		this.gender=  list.get(5);
		this.sign=  list.get(6);
		this.location_x=  list.get(7);
		this.location_y=  list.get(8);
		this.location_time=  list.get(9);
		this.location_status=  list.get(10);
		this.modifed=  list.get(11);
	}

	public void fromJson(JSONObject json){
		token = json.optString("token");
		id = json.optString("id");
		username = json.optString("username");
		passwd = json.optString("passwd");
		nickname = json.optString("nickname");
		avatar = json.optString("avatar");
		gender = json.optString("gender");
		sign = json.optString("sign");
		location_x = json.optString("location_x");
		location_y = json.optString("location_y");
		location_time = json.optString("location_time");
		location_status = json.optString("location_status");
		modifed = json.optString("modifed");
	}
	public JSONObject toJson(){
		JSONObject json = new JSONObject();
		try {
			json.put("token", token);
			json.put("id", id);
			json.put("username", username);
			json.put("passwd", passwd);
			json.put("nickname", nickname);
			json.put("avatar", avatar);
			json.put("gender", gender);
			json.put("sign", sign);
			json.put("location_x", location_x);
			json.put("location_y", location_y);
			json.put("location_time", location_time);
			json.put("location_status", location_status);
			json.put("modifed", modifed);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return json;
	}
	public static User createFromJson(JSONObject json) {
		User user = new User();
		user.fromJson(json);
		return user;
	}
	public static enum Gender {
		Male(0), Female(1);

		int value;

		Gender(int value) {
			this.value = value;
		}

		public int getValue() {
			return value;
		}

		public static Gender fromInt(int index) {
			return values()[index];
		}
	}


	public static User curUser() {
		if (curUser == null) {
			curUser = getCurrentUser(User.class);
		}
		return curUser;
	}

	public static void setCurUser(User curUser) {
		User.curUser = curUser;
	}

	public static String curUserId() {
		User user = curUser();
		if (user != null) {
			return user.getObjectId();
		} else {
			return null;
		}
	}

	public AVFile getAvatar() {
		return getAVFile(AVATAR);
	}

	public void setAvatar(AVFile avatar) {
		put(AVATAR, avatar);
	}

	public String getAvatarUrl() {
		AVFile avatar = getAvatar();
		if (avatar != null) {
			return avatar.getUrl();
		} else {
			return null;
		}
	}

	public void addFriend(User user) {
		getRelation(FRIENDS).add(user);
	}


	public void removeFriend(User user) {
		getRelation(FRIENDS).remove(user);
	}

	public AVGeoPoint getLocation() {
		return getAVGeoPoint(LOCATION);
	}

	public void setLocation(AVGeoPoint location) {
		put(LOCATION, location);
	}

	public Gender getGender() {
		int genderInt = getInt(GENDER);
		return Gender.fromInt(genderInt);
	}

	public void setGender(Gender gender) {
		put(GENDER, gender.getValue());
	}


	public String getSortLetters() {
		return sortLetters;
	}

	public void setSortLetters(String sortLetters) {
		this.sortLetters = sortLetters;
	}
}
